<?php

class M_public_function extends CI_Model{
	
	function M_public_function(){
		parent::__construct();
	}
	
	function clear_cache(){
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache");		
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	}
	

		
	function get_info(){
		$sql="SELECT * FROM tbl_s_info";
		$query = $this->db->query($sql);
		return $query->row();
	}
	
	function get_param($param1){
		$sql="SELECT * FROM m_setup WHERE param1 = '".mysql_real_escape_string($param1)."'";
		$query = $this->db->query($sql);
		return $query->row();
	}
	
	function get_purcm(){
		$sql="SELECT * FROM m_setup WHERE setup_id = 9";
		$query = $this->db->query($sql);
		return $query->row('vvalue1');
	}
	
	
	function get_usergroups_list(){
		$sql="SELECT group_id,group_name FROM tbl_s_usergroups WHERE group_active='Y'";
		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}

	function get_nama_bulan($number){
		$in_month=(int)$number;
		if($in_month==1){
			return "Januari";
		}else if($in_month==2){
			return "Pebruari";
		}else if($in_month==3){
			return "Maret";
		}else if($in_month==4){
			return "April";
		}else if($in_month==5){
			return "Mei";
		}else if($in_month==6){
			return "Juni";
		}else if($in_month==7){
			return "Juli";
		}else if($in_month==8){
			return "Agustus";
		}else if($in_month==9){
			return "September";
		}else if($in_month==10){
			return "Oktober";
		}else if($in_month==11){
			return "Nopember";
		}else if($in_month==12){
			return "Desember";
		}else{
			return "Error";
		}	
	}
	
	function get_month_tilnow(){
		$thismonth=date('m');
		$thismonth=(int)$thismonth;
		for($i=0;$i<$thismonth;$i++){
			$data[$i]["month_number"]=$i+1;
			$data[$i]["month_name"]=$this->get_nama_bulan($i+1);
			
		}
		if($thismonth>0){
			$jsonresult = json_encode($data);
			return '({"total":"'.$thismonth.'","results":'.$jsonresult.'})';
		}else{
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_kode_1($table,$field,$pattern,$length){
		$len_pattern=strlen($pattern);
		$len_lpad=$length-$len_pattern;
		$sql="select concat(left(max(".$field."),".$len_pattern."),LPAD((right(max(".$field."),".$len_lpad.")+1),".$len_lpad.",0)) as max_key from ".$table." where ".$field." like '".$pattern."%'";
		$query=$this->db->query($sql);
		if($query->num_rows()){
			$data=$query->row();
			$kode=$data->max_key;
			if(is_null($kode))
			{
				$pad="";
				for($i=1;$i<$len_lpad;$i++)
					$pad.="0";
				$kode=$pattern.$pad."1";
			}
			return $kode;
		}else{
			$pad="";
			for($i=1;$i<$len_lpad;$i++)
				$pad.="0";
			$kode=$pattern.$pad."1";
			return $kode;
		}
	}
	
	function get_kode_2($table,$field,$pattern_g,$pattern_j,$length){
		$len_pattern=strlen($pattern_g.$pattern_j);
		$len_lpad=$length-$len_pattern;
		$sql="select concat(left(max(".$field."),".$len_pattern."),LPAD((right(max(".$field."),".$len_lpad.")+1),".$len_lpad.",0)) as max_key from ".$table." where ".$field." like '".$pattern_g.$pattern_j."%'";
		$query=$this->db->query($sql);
		if($query->num_rows()){
			$data=$query->row();
			$kode=$data->max_key;
			if(is_null($kode))
			{
				$pad="";
				for($i=1;$i<$len_lpad;$i++)
					$pad.="0";
				$kode=$pattern_g.$pattern_j.$pad."1";
			}
			return $kode;
		}else{
			$pad="";
			for($i=1;$i<$len_lpad;$i++)
				$pad.="0";
			$kode=$pattern_g.$pattern_j.$pad."1";
			return $kode;
		}
	}
	
	function get_kode($table,$field,$pattern,$length){
		$len_pattern=strlen($pattern);
		$len_lpad=$length-$len_pattern;
		$sql="select concat(left(max(".$field."),".$len_pattern."),LPAD((right(max(".$field."),".$len_lpad.")+1),".$len_lpad.",0)) as max_key from ".$table." where ".$field." like '".$pattern."%'";
		$query=$this->db->query($sql);
		if($query->num_rows()){
			$data=$query->row();
			$kode=$data->max_key;
			if(is_null($kode))
			{
				$pad="";
				for($i=1;$i<$len_lpad;$i++)
					$pad.="0";
				$kode=$pattern.$pad."1";
			}
			return $kode;
		}else{
			$pad="";
			for($i=1;$i<$len_lpad;$i++)
				$pad.="0";
			$kode=$pattern.$pad."1";
			return $kode;
		}
	}
	
	
	
	function get_logo_file(){
		$sql = "SELECT info_logo FROM tbl_s_info LIMIT 1";
		$rs = $this->db->query($sql);
		if ($rs->num_rows()>0){
			$record = $rs->row();
			return $record->info_logo;
		}else {
			return "";
		}
	}
	
	function get_div($filter){
		$query = "SELECT div_id, div_name, div_active FROM m_div";
		
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (div_name LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_branch($filter){
		$query = "SELECT branch_id, branch_name, branch_active, dvo_id, dvo_name 
				FROM m_branch JOIN m_dvo ON(m_dvo.dvo_id = m_branch.m_dvo_id)";
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (div_name LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_procurement($filter){
		$query = "SELECT param2 as procurement_name 
				FROM m_setup WHERE param3 = 'PURCM'";
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (param2 LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	
	function get_payment($filter){
		$query = "SELECT payment_id, payment_name
				FROM m_payment";
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (div_name LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	
	function m_div_create($div_name, $div_active){
		$split_div_name = explode(" ", $div_name);
		$div_name = "";
		for($i=0; $i<sizeof($split_div_name); $i++){
			if($split_div_name[$i] != ""){
				if($i>0){
					$div_name .= " ";
				}
				$div_name .= $split_div_name[$i];
			}
		}
		
		//checking $div_name apakah sudah ada dalam database?
		//Jika "Ada" ==> return '0';
		//Jika "Tidak Ada" ==> return $this->db->insert_id();
		$sql = "SELECT div_id FROM m_div WHERE LOWER(div_name)='".strtolower($div_name)."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			$record = $rs->row_array();
			return $record['div_id'];
		}
		
		$data = array(
				"div_name"=>$div_name,
				"div_active"=>$div_active,
				"div_created_by"=>$_SESSION[SESSION_USERID]
		);
		$this->db->insert('m_div', $data);
		if($this->db->affected_rows())
			return $this->db->insert_id();
		else
			return 0;
	}
	
	function get_project($filter, $m_branch){
		$query = "SELECT project_id, project_name, project_code, m_div_id
				FROM t_project
				WHERE m_branch_id=".$m_branch;
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (project_name LIKE '%".addslashes($filter)."%' )";
		}
		
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_job($filter, $div_id){
	
		$sql = "call prepare_tablesplit";
		$this->db->query($sql);
		$sql = "SELECT job_id, job_name, m_div_id FROM m_job";
		$query = $this->db->query($sql);
		foreach($query->result()  as $result){
			$sql = "call split_string('".$result->m_div_id."',',',".$result->job_id.");";
			$this->db->query($sql);
		}
		$query = "SELECT job_id, job_name, m_div_id FROM m_job 
		LEFT JOIN splitResults ON job_id = split_id
		WHERE split_value=".$div_id;
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (job_name LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_job_bybranchproject($branch, $project){
		$query = "SELECT div_name, t_rapk.m_div_id as div_id, rapk_id, job_id, job_name,  rapk_total  AS rapkdet_total
				FROM t_rapk JOIN t_rapkdet ON(t_rapkdet.t_rapk_id = t_rapk.rapk_id)
					JOIN m_job ON(m_job.job_id = t_rapk.m_job_id)
					LEFT JOIN m_div ON(t_rapk.m_div_id  = div_id)
				WHERE t_rapk.m_branch_id=".$branch." AND t_rapk.t_project_id=".$project."
				GROUP BY t_rapk_id";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_job_bybranchprojectBelumMenang($branch, $project){
		$query = "SELECT div_name, t_rapk.m_div_id as div_id, rapk_id, job_id, job_name,  rapk_total  AS rapkdet_total
				FROM t_rapk JOIN t_rapkdet ON(t_rapkdet.t_rapk_id = t_rapk.rapk_id)
					JOIN m_job ON(m_job.job_id = t_rapk.m_job_id)
					LEFT JOIN m_div ON(t_rapk.m_div_id  = div_id)
					LEFT JOIN	(	SELECT m_job_id, t_project_id FROM t_nego join t_negodet on nego_id = t_nego_id
			where negodet_menang = 'Y' ) job_menang
		ON t_rapk.m_job_id = job_menang.m_job_id AND t_rapk.t_project_id = job_menang.t_project_id
				WHERE t_rapk.m_branch_id=".$branch." AND t_rapk.t_project_id=".$project."
						AND job_menang.m_job_id is null
				GROUP BY t_rapk_id";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_jobdet_all($task,$master_id,$selected_id,$filter,$start,$end){

		if($filter<>""){
			$task="all";
		}
		
		$sql = "SELECT job_id, job_name, jobdet_id, jobdet_name FROM m_job JOIN m_jobdet ON(m_jobdet.m_job_id = m_job.job_id)";
		
		if($task=='detail'){
			if (($master_id<>"") && ($master_id<>0)){
				$sql .=preg_match("/\bWHERE\b/i",$sql)? " AND ":" WHERE ";
				$sql .= " (m_jobdet.m_job_id = ".$master_id." )";
			}
		
			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
		
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		
		}else if($task=='selected'){
			$selected_id_list = "";
			for($i = 0; $i < sizeof($selected_id); $i++){
				if($i>0){
					$selected_id_list .= ",";
				}
				$selected_id_list .= $selected_id[$i];
			}
		
			if($selected_id_list!=="")
			{
				//$selected_id=substr($selected_id,0,strlen($selected_id)-1);
				$sql.=(preg_match("/\bWHERE\b/i",$sql)?" AND ":" WHERE ")." jobdet_id IN(".$selected_id_list.")";
			}
		
			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
		
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		
		}else{
			if ($filter<>""){
				$sql .=preg_match("/\bWHERE\b/i",$sql)? " AND ":" WHERE ";
				$sql .= " (m_jobdet.jobdet_name LIKE '%".addslashes($filter)."%' )";
			}
		
			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
		
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
	}
	
	function get_vendor($filter, $vendor_type, $start, $end){
		$query = "SELECT vendor_id, vendor_name, vendor_address
				FROM m_vendor
				WHERE vendor_type='".$vendor_type."'";
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (project_name LIKE '%".addslashes($filter)."%' )";
		}
	
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		$limit = $query." LIMIT ".$start.", ".$end;
		$result = $this->db->query($limit);
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
		
	function terbilang($num) {
	  $digits = array(
		0 => "nol",
		1 => "satu",
		2 => "dua",
		3 => "tiga", 
		4 => "empat",
		5 => "lima",
		6 => "enam",
		7 => "tujuh",
		8 => "delapan",
		9 => "sembilan");
	  $orders = array(
		 0 => "",
		 1 => "puluh",
		 2 => "ratus",
		 3 => "ribu",
		 6 => "juta",
		 9 => "miliar",
		12 => "triliun",
		15 => "kuadriliun");

	  $is_neg = $num < 0; $num = "$num";

	  //// angka di kiri desimal

	  $int = ""; if (preg_match("/^[+-]?(\d+)/", $num, $m)) $int = $m[1];
	  $mult = 0; $wint = "";

	  // ambil ribuan/jutaan/dst
	  while (preg_match('/(\d{1,3})$/', $int, $m)) {
		
		// ambil satuan, puluhan, dan ratusan
		$s = $m[1] % 10; 
		$p = ($m[1] % 100 - $s)/10;
		$r = ($m[1] - $p*10 - $s)/100;
		
		// konversi ratusan
		if ($r==0) $g = "";
		elseif ($r==1) $g = "se$orders[2]";
		else $g = $digits[$r]." $orders[2]";
		
		// konversi puluhan dan satuan
		if ($p==0) {
		  if ($s==0);
		  elseif ($s==1) $g = ($g ? "$g ".$digits[$s] :
									($mult==0 ? $digits[1] : "se"));
		  else $g = ($g ? "$g ":"") . $digits[$s];
		} elseif ($p==1) {
		  if ($s==0) $g = ($g ? "$g ":"") . "se$orders[1]";
		  elseif ($s==1) $g = ($g ? "$g ":"") . "sebelas";
		  else $g = ($g ? "$g ":"") . $digits[$s] . " belas";
		} else {
		  $g = ($g ? "$g ":"").$digits[$p]." puluh".
			   ($s > 0 ? " ".$digits[$s] : "");
		}

		// gabungkan dengan hasil sebelumnya
		$wint = ($g ? $g.($g=="se" ? "":" ").$orders[$mult]:"").
				($wint ? " $wint":""); 
		
		// pangkas ribuan/jutaan/dsb yang sudah dikonversi
		$int = preg_replace('/\d{1,3}$/', '', $int);
		$mult+=3;
	  }
	  if (!$wint) $wint = $digits[0];
	  
	  //// angka di kanan desimal

	  $frac = ""; if (preg_match("/\.(\d+)/", $num, $m)) $frac = $m[1];
	  $wfrac = "";
	  for ($i=0; $i<strlen($frac); $i++) {
		$wfrac .= ($wfrac ? " ":"").$digits[substr($frac,$i,1)];
	  }
	  
	  return ($is_neg ? "minus ":"").$wint.($wfrac ? " koma $wfrac":"");
	}
	
	function get_komp_byfilter($branch, $project, $job){
		$query = "SELECT komp_id, vendor_id, vendor_name, vendor_address, komp_date, komp_no, SUM(kompdet_vol * kompdet_price) AS komp_total, 
					SUM(kompdet_vol * kompdet_rapkprice) AS rapk_total
				FROM t_komp JOIN t_kompdet ON(t_kompdet.t_komp_id = t_komp.komp_id)
				LEFT JOIN m_vendor ON(m_vendor.vendor_id = t_komp.m_vendor_id)
				WHERE t_komp.m_branch_id = ".$branch." AND t_komp.t_project_id = ".$project." AND t_komp.m_job_id = ".$job."
				GROUP BY t_kompdet.t_komp_id";
		
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_unit($filter){
		$query = "SELECT unit_id, unit_name
				FROM m_unit";
	
		// For simple search
		if ($filter<>""){
			$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
			$query .= " (unit_name LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_kontrak($filter,$start,$end,$branch_id,$project_id){
		$sql = "SELECT kontrak_id, kontrak_no, kontrak_date, kontrak_begin, kontrak_end, (TO_DAYS(kontrak_end) - TO_DAYS(kontrak_begin)) AS kontrak_jangkawaktu,
					project_code, project_name, project_p1, vendor_id, vendor_name, vendor_address, job_id, job_name, vendor_cp
				FROM t_kontrak JOIN t_spk ON(t_spk.spk_id = t_kontrak.t_spk_id)
				JOIN t_komp ON(t_komp.komp_id = t_spk.t_komp_id)
				LEFT JOIN t_project ON(t_project.project_id = t_komp.t_project_id)
				LEFT JOIN m_vendor ON(m_vendor.vendor_id = t_komp.m_vendor_id)
				LEFT JOIN m_job ON(m_job.job_id = t_komp.m_job_id)
				WHERE t_komp.m_branch_id = ".$branch_id." AND t_komp.t_project_id = ".$project_id;
	
		// For simple search
		if ($filter<>""){
			$sql .=preg_match("/\bWHERE\b/i",$sql)? " AND ":" WHERE ";
			$sql .= " (kontrak_no LIKE '%".addslashes($filter)."%' )";
		}
			
		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		$limit = $sql." LIMIT ".$start.", ".$end;
		$result = $this->db->query($limit);
			
		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_ppn(){
		$sql = "SELECT nvalue1 FROM m_setup WHERE param1 = 'PPN' LIMIT 1";
		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		if($nbrows>0){
			$record = $result->row();
			return $record->nvalue1;
		} else {
			return 0;
		}
	}
	
	function getBulan($bln){
		switch  ($bln){
		case  1:
		return  "Januari";
		break;
		case  2:
		return  "Februari";
		break;
		case  3:
		return  "Maret";
		break;
		case  4:
		return  "April";
		break;
		case  5:
		return  "Mei";
		break;
		case  6:
		return  "Juni";
		break;
		case  7:
		return  "Juli";
		break;
		case  8:
		return  "Agustus";
		break;
		case  9:
		return  "September";
		break;
		case  10:
		return  "Oktober";
		break;
		case  11:
		return  "November";
		break;
		case  12:
		return  "Desember";
		break;
		}
	}
	function getDay($day){
		switch($day){
			case 1:
				return "Senin";
			break;
			case 2:
				return "Selasa";
			break;
			case 3:
				return "Rabu";
			break;
			case 4:
				return "Kamis";
			break;
			case 5:
				return "Jumat";
			break;
			case 6:
				return "Sabtu";
			break;
			case 7:
				return "Minggu";
			break;
		}
	}
	function indonesiaDateFormat($tgl){
		$day = $this->getDay(substr($tgl, 0,1));
		$tanggal  =  substr($tgl,10,2);
		$bulan	=  $this->getBulan(substr($tgl,7,2));
		$tahun	=  substr($tgl,2,4);
		return  $day.', '.$tanggal.' '.$bulan.' '.$tahun;
	}
	function indonesiaDate($tgl){
		$tanggal  =  substr($tgl,8,2);
		$bulan	=  $this->getBulan(substr($tgl,5,2));
		$tahun	=  substr($tgl,0,4);
		return $tanggal.' '.$bulan.' '.$tahun;
	}
	function dateTerbilang($tgl){
		$day = $this->getDay(substr($tgl, 0,1));
		$tanggal  =  ucwords($this->terbilang(substr($tgl,10,2)));
		$bulan	=  $this->getBulan(substr($tgl,7,2));
		$tahun	=  ucwords($this->terbilang(substr($tgl,2,4)));
		return  $day.', '.$tanggal.' '.$bulan.' '.$tahun;
	}
	
	function dateTerbilangUnu($tgl){
		$day = $this->getDay(substr($tgl, 0,1));
		$tanggal  =  ucwords($this->terbilang(substr($tgl,10,2)));
		$bulan	=  $this->getBulan(substr($tgl,7,2));
		$tahun	=  ucwords($this->terbilang(substr($tgl,2,4)));
		return  $day.' Tanggal '.$tanggal.' Bulan '.$bulan.' Tahun '.$tahun;
	}
	
	function get_setupAmbang(){

		$sql = " SELECT nvalue1 FROM m_setup WHERE param1 = 'AMBANG'";
		$query = $this->db->query($sql);
		$nilaiAmbang = $query->row('nvalue1');
		return $nilaiAmbang;
	}
	
	function get_kontrakcontent($jenis_kontrak, $type_konten,$nilai_kontrakm){
		/* mengambil data ambang nilai kontrak */
		$sql = " SELECT nvalue1 FROM m_setup WHERE param1 = 'AMBANG'";
		$query = $this->db->query($sql);
		$nilaiAmbang = $query->row('nvalue1');
		
		if($type_konten != "Footer"){
			$kuery = "SELECT bvalue1 as konten";
		}else{
			if ($nilai_kontrakm > $nilaiAmbang ){
				$kuery = "SELECT bvalue2 as konten ";
			}else{
				$kuery = "SELECT bvalue3 as konten ";
			}
		}
		$kuery .= " FROM m_setup WHERE param1 = '" . $jenis_kontrak . "'";
		$query = $this->db->query($kuery);
		if($query->num_rows() > 0){
			$row = $query->row();
			$result = $row->konten;
			return $result;
		}
	}
	
	function getDivIdByName($divName){
		$this->db->where('div_name',$divName);
		$query = $this->db->get('m_div');
		return $query->row('div_id');
	}
    
    function grafikBarang(){
        $sql = "SELECT job_id jobdet_id, job_name jobdet_name, kompdet_vol, unit_name, SUM(kompdet_price) kompdet_price, kontrak_date, vendor_name, project_name FROM `t_spkdet` 
            LEFT JOIN t_kontrak ON t_spkdet.t_spk_id = t_kontrak.t_spk_id
            LEFT JOIN t_kompdet ON  t_kompdet_id = kompdet_id
            LEFT JOIN t_komp  ON  t_komp_id = komp_id
            LEFT JOIN m_jobdet ON  m_jobdet_id = jobdet_id
            LEFT JOIN m_unit ON  kompdet_unit = unit_id
            LEFT JOIN m_vendor ON  vendor_id = m_vendor_id
            LEFT JOIN t_project ON  project_id = t_project_id
            LEFT JOIN m_job ON  m_jobdet.m_job_id = job_id
            WHERE job_id is not null
            GROUP BY job_name, kontrak_date
            ORDER BY kontrak_date";
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }
    
    function grafikBarangDetail($barang){
        $sql = "SELECT jobdet_id, jobdet_name, kompdet_vol, unit_name, AVG(kompdet_price) kompdet_price, kontrak_date, vendor_name, project_name FROM `t_spkdet` 
            LEFT JOIN t_kontrak ON t_spkdet.t_spk_id = t_kontrak.t_spk_id
            LEFT JOIN t_kompdet ON  t_kompdet_id = kompdet_id
            LEFT JOIN t_komp  ON  t_komp_id = komp_id
            LEFT JOIN m_jobdet ON  m_jobdet_id = jobdet_id
            LEFT JOIN m_unit ON  kompdet_unit = unit_id
            LEFT JOIN m_vendor ON  vendor_id = m_vendor_id
            LEFT JOIN t_project ON  project_id = t_project_id
            LEFT JOIN m_job ON  m_jobdet.m_job_id = job_id
            WHERE job_id = $barang
            GROUP BY jobdet_name, kontrak_date
            ORDER BY kontrak_date";
        $query = $this->db->query($sql);
        $result = $query->result();
        $data = array();
        $categories = array();
        foreach(@$result as $row ){
            $kontrak_date= date("d-m-Y",strtotime($row->kontrak_date));
            $jobdet_name= $row->jobdet_name;
            $kompdet_price= $row->kompdet_price * 1;
            $data[$kontrak_date][$jobdet_name] = $kompdet_price;
            if (!in_array($kontrak_date,$categories )){
                $categories[] =  $kontrak_date;
            }
        }
        
        return array($data, $categories);
    }
}

?>